﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;

namespace Feniks
{
    public partial class frmMasura : DevExpress.XtraEditors.XtraForm
    {
        List<MasuraOlcu> _secilenOlcu = new List<MasuraOlcu>();

        public frmMasura()
        {
            InitializeComponent();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            frmMasuraGramaj ms = new frmMasuraGramaj();
            ms.MasuraID = -1;
            if (cmbMasura.SelectedIndex > -1)
            {
                MasuraTanimi item = (MasuraTanimi)((ComboItem)cmbMasura.SelectedItem).Value;
                if (item != null)
                {
                    ms.MasuraID = item.ID;
                }
            }
            if (ms.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                foreach (var item in ms.Gramajlar)
                {
                    if (!string.IsNullOrEmpty(item.Olcu))
                    {
                        _secilenOlcu.Add(new MasuraOlcu()
                        {
                            MasuraID = -1,
                            Metre = item.Olcu,
                            Gram = item.Gram
                        });
                    }
                }
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(cmbMasura.Text))
            {
                using (DataEntities ent = new DataEntities())
                {
                    var sonuc = ent.MasuraTanimlari.Where(x => x.MasuraAdi == cmbMasura.Text).FirstOrDefault();
                    if (sonuc != null)
                    {
                        sonuc.MasuraAdi = cmbMasura.Text;
                    }
                    else
                    {
                        ent.AddToMasuraTanimlari(new MasuraTanimi()
                        {
                            MasuraAdi = cmbMasura.Text
                        });
                    }
                    ent.SaveChanges();

                    sonuc = ent.MasuraTanimlari.Where(x => x.MasuraAdi == cmbMasura.Text).FirstOrDefault();
                    if (sonuc != null)
                    {
                        var liste = ent.MasuraOlculeri.Where(x => x.MasuraID == sonuc.ID).ToList();
                        foreach (var o in liste)
                        {
                            ent.DeleteObject(o);
                        }
                        ent.SaveChanges();

                        foreach (var ol in _secilenOlcu)
                        {
                            ol.MasuraID = sonuc.ID;
                            ent.AddToMasuraOlculeri(ol);
                        }
                        ent.SaveChanges();
                    }
                }
                cmbMasura.SelectedIndex = -1;
                _secilenOlcu.Clear();
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(cmbMasura.Text))
            {
                using (DataEntities ent = new DataEntities())
                {
                    var sonuc = ent.MasuraTanimlari.Where(x => x.MasuraAdi == cmbMasura.Text).FirstOrDefault();
                    if (sonuc != null)
                    {
                        var liste = ent.MasuraOlculeri.Where(x => x.MasuraID == sonuc.ID).ToList();
                        foreach (var o in liste)
                        {
                            ent.DeleteObject(o);
                        }
                        ent.DeleteObject(sonuc);
                        ent.SaveChanges();
                    }
                }
                cmbMasura.SelectedIndex = -1;
                _secilenOlcu.Clear();
            }
        }

        private void frmMasura_Load(object sender, EventArgs e)
        {
            using (DataEntities ent = new DataEntities())
            {
                var liste = ent.MasuraTanimlari.ToList();
                foreach (var item in liste)
                {
                    cmbMasura.Properties.Items.Add(new ComboItem()
                    {
                        Text = item.MasuraAdi,
                        Value = item
                    });
                }
            }
        }

        private void cmbMasura_SelectedIndexChanged(object sender, EventArgs e)
        {
            _secilenOlcu.Clear();
        }
    }
}